package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.modular.system.model.MyGpc;
import com.stylefeng.guns.modular.system.model.MyGpcHistory;
import com.stylefeng.guns.modular.system.service.IMyGpcHistoryService;
import com.stylefeng.guns.modular.system.service.IMyGpcService;
import com.stylefeng.guns.modular.system.warpper.MyGpcWarpper;

/**
 * 我的股票池控制器
 *
 * @author fengshuonan
 * @Date 2018-07-02 14:55:39
 */
@Controller
@RequestMapping("/myGpc")
public class MyGpcController extends BaseController {

    private String PREFIX = "/system/myGpc/";

    @Autowired
    private IMyGpcService myGpcService;
    
    @Autowired
    private IMyGpcHistoryService myGpcHistoryService;

    /**
     * 跳转到我的股票池首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "myGpc.html";
    }

    /**
     * 跳转到添加我的股票池
     */
    @RequestMapping("/myGpc_add")
    public String myGpcAdd() {
        return PREFIX + "myGpc_add.html";
    }
    
    @RequestMapping("/myGpc_ccadd")
    public String myGpcccAdd() {
        return PREFIX + "myGpc_ccadd.html";
    }
    
    @RequestMapping("/myGpc_clear_ccadd")
    public String myGpcClearccAdd() {
        return PREFIX + "myGpc_clear_ccadd.html";
    }

    /**
     * 跳转到修改我的股票池
     */
    @RequestMapping("/myGpc_update/{myGpcId}")
    public String myGpcUpdate(@PathVariable Integer myGpcId, Model model) {
        MyGpc myGpc = myGpcService.selectById(myGpcId);
        model.addAttribute("item",myGpc);
        LogObjectHolder.me().set(myGpc);
        return PREFIX + "myGpc_edit.html";
    }

    /**
     * 获取我的股票池列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String date,String dm) {
    	Page<MyGpc> page = new PageFactory<MyGpc>().defaultPage();
    	List<Map<String, Object>> gdhs = myGpcService.selectMapsByDm(page,date,dm);
    	page.setRecords((List<MyGpc>) new MyGpcWarpper(gdhs).warp());
        return super.packForBT(page);
    }

    /**
     * 新增我的股票池
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(MyGpc myGpc) {
        myGpcService.insertMyGpc(myGpc);
        return SUCCESS_TIP;
    }
    
    @RequestMapping(value = "/ccadd")
    @ResponseBody
    public Object ccadd(String gScode,String gTdate) {
    	MyGpcHistory gpcHistory = new MyGpcHistory();
    	gpcHistory.setgBuyDate(DateUtil.parse(gTdate, "yyyy-MM-dd"));
    	gpcHistory.setgScode(gScode);
    	ShiroUser shiroUser = ShiroKit.getUser();
    	gpcHistory.setgUserid(shiroUser.getId());
    	gpcHistory.setgCcStatus(1l);
    	myGpcHistoryService.insert(gpcHistory);
        return SUCCESS_TIP;
    }
    
    @RequestMapping(value = "/clearccadd")
    @ResponseBody
    public Object clearccadd(String gScode,String gTdate) {
    	ShiroUser shiroUser = ShiroKit.getUser();
    	Wrapper<MyGpcHistory> wrapper = new EntityWrapper<MyGpcHistory>();
    	wrapper.eq("g_scode", gScode);
    	wrapper.eq("g_userid", shiroUser.getId());
    	wrapper.eq("g_cc_status", 1);
    	MyGpcHistory gpcHistory = myGpcHistoryService.selectOne(wrapper);
    	if(gpcHistory != null){
    		gpcHistory.setgSellerDate(DateUtil.parse(gTdate, "yyyy-MM-dd"));
    		gpcHistory.setgCcStatus(2l);
    		myGpcHistoryService.updateById(gpcHistory);
    	}
        return SUCCESS_TIP;
    }

    /**
     * 删除我的股票池
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long id) {
        myGpcService.deleteById(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改我的股票池
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(MyGpc myGpc) {
        myGpcService.updateById(myGpc);
        return SUCCESS_TIP;
    }

    /**
     * 我的股票池详情
     */
    @RequestMapping(value = "/detail/{myGpcId}")
    @ResponseBody
    public Object detail(@PathVariable("myGpcId") Integer myGpcId) {
        return myGpcService.selectById(myGpcId);
    }
}
